#include "widget.h"
#include "ui_widget.h"
#include<QByteArray>
#include<QFile>
#include<QDebug>
#include<QTimer>
#include<QDateTime>
#include<QPixmap>
#include<QPalette>
#include<QBrush>
#include<QUrlQuery>
Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
   // this->resize(600,400);
    _socket = new QTcpSocket;//默认父对象为当前窗体
    _socket->connectToHost(QHostAddress("119.91.109.180"),8344);//客户端连接服务端
    QByteArray arr("cmd=1&uid=1213ba0c33d4409488aed2c07e1c63e6&topic=send\r\n");
    _socket->write(arr);
    connect(_socket,SIGNAL(readyRead()),this,SLOT(recv_info()));//关联接收
    QPixmap pixmap = QPixmap(":/1.jpg");
    QPalette pa(this->palette());
    pa.setBrush(QPalette::Background,QBrush(pixmap));
    this->setPalette(pa);
    timer = new QTimer(this);
    connect(timer,SIGNAL(timeout()),this,SLOT(showtime()));
    ui->lcdNumber->setDigitCount(19);
    ui->lcdNumber->setSegmentStyle(QLCDNumber::Filled);
    ui->lcdNumber->setStyleSheet("border:7px solid red;color:red;"
                                 "background:pink");
    timer->start(1000);
}

Widget::~Widget()
{
    delete ui;
}
void Widget::showtime()
{
    QDateTime dt = QDateTime::currentDateTime();
    QString str = dt.toString("yyyy-MM-dd-hh:mm:ss");
    ui->lcdNumber->display(str);
}

void Widget::on_pushButton_clicked()//读文件
{

    QFile file("./1.txt");
    if(file.exists())
    {
        qDebug()<<"文件已存在"<<endl;
    }
    else
    {
        qDebug()<<"文件不存在"<<endl;
    }
    if(!file.open(QIODevice::ReadOnly))
    {
        qDebug()<<"打开失败"<<endl;
    }
    else
    {
        qDebug()<<"打开成功"<<endl;
    }
    QByteArray arr = file.readAll();
    ui->textEdit->setText(QString(arr).toUtf8().data());
    /*
    int line = 1;
    while (!file.atEnd())
    {
        char buf[1024];
        qint64 length = file.readLine(buffer,1024);
        if(length != -1)
        {
            qDebug()<<line++<<":"<<buf;
        }
    }*/
    file.close();
}

void Widget::on_pushButton_2_clicked()
{
    QFile file("./1.txt");
    if(file.exists())
    {
        qDebug()<<"文件已存在"<<endl;
    }
    else
    {
        qDebug()<<"文件不存在"<<endl;
    }
    if(!file.open(QIODevice::WriteOnly|QIODevice::Truncate))
    {
        qDebug()<<"打开失败"<<endl;
    }
    else
    {
        qDebug()<<"打开成功"<<endl;
    }
    QString buf = ui->textEdit->toPlainText();
    qDebug()<<buf<<endl;
    file.write(buf.toUtf8());
    ui->textEdit->clear();
    file.close();
}

void Widget::recv_info()
{

    ui->textEdit->clear();
    QByteArray arr = _socket->readAll();
    if(arr.isEmpty())
    {
        return;
    }
    qDebug()<<"recv:"<<arr;
    const QString str(arr);
    QUrlQuery q(str);
    const QString msg = q.queryItemValue("msg");
    ui->textEdit->setText(msg.section("%",0,0).toUtf8());
}

void Widget::on_pushButton_4_clicked()
{
    ui->textEdit->clear();
    QByteArray arr("cmd=2&uid=1213ba0c33d4409488aed2c07e1c63e6&topic=dunshi&msg=led_on\r\n");
    _socket->write(arr);
}

void Widget::on_pushButton_3_clicked()
{
    ui->textEdit->clear();
    QByteArray arr("cmd=2&uid=1213ba0c33d4409488aed2c07e1c63e6&topic=dunshi&msg=led_off\r\n");
    _socket->write(arr);
}
